﻿using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Audio;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.Projectiles.Ammo
{
    public class GraniteEnergyBullet : ModProjectile
    {
        public override void SetDefaults()
        {
            Projectile.friendly = true;
            Projectile.hostile = false;
            Projectile.DamageType = DamageClass.Throwing;
            Projectile.penetrate = 1;
            Projectile.width = 14;
            Projectile.height = 14;
            Projectile.extraUpdates = 1;
            Projectile.ignoreWater = true;
        }

        public override Color? GetAlpha(Color lightColor)
        {
            return Color.White;
        }

        public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone)
        {
            Projectile.usesLocalNPCImmunity = true;
            Projectile.localNPCImmunity[target.whoAmI] = 10;
            target.immune[Projectile.owner] = 0;
        }


        public override void AI()
        {
            Projectile.rotation += Projectile.velocity.X * 0.1f;
            Projectile.velocity.Y = Projectile.velocity.Y + 0.1f;

            if (Projectile.velocity.X > 18f)
            {
                Projectile.velocity.X = 18f;
            }
            if (Projectile.velocity.X < -18f)
            {
                Projectile.velocity.X = -18f;
            }

            if (Projectile.velocity.Y > 18f)
            {
                Projectile.velocity.Y = 18f;
            }
            if (Projectile.velocity.Y < -18f)
            {
                Projectile.velocity.Y = -18f;
            }

            Lighting.AddLight(Projectile.position, 0.6f, 0.75f, 1f);

            int num97 = Dust.NewDust(new Vector2(Projectile.position.X, Projectile.position.Y), Projectile.width, Projectile.height, 59, 0f, 0f, 100, default, 1f);
            Main.dust[num97].noGravity = true;
        }

        public override void OnKill(int timeLeft)
        {
            Collision.HitTiles(Projectile.position, Projectile.velocity, Projectile.width, Projectile.height);
            SoundEngine.PlaySound(SoundID.Dig, Projectile.position);
            for (int num526 = 0; num526 < 20; num526++)
            {
                int granite = Dust.NewDust(new Vector2(Projectile.position.X, Projectile.position.Y), Projectile.width, Projectile.height, 59, 0f, 0f, 100, default, 1f);
                Main.dust[granite].velocity *= 1.5f;
            }

            Vector2 perturbedSpeed = new Vector2(3f, 3f).RotatedByRandom(MathHelper.ToRadians(360));
            if (Main.myPlayer == Projectile.owner)
            {
                Projectile.NewProjectile(Projectile.GetSource_Death(), Projectile.Center.X, Projectile.Center.Y, perturbedSpeed.X, perturbedSpeed.Y, ModContent.ProjectileType<GraniteEnergySlingBulletSurge>(), Projectile.damage / 2, 0f, Projectile.owner, 0f, 0f);
            }
        }
    }
}
